package com.ruoyi.apm.dao;

import java.util.List;
import java.util.Map;

import com.ruoyi.apm.model.HttpSequence;
import com.ruoyi.apm.model.HttpSystem;
import com.ruoyi.common.utils.tree.TreeItem;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface HttpSequenceMapper {

	@Insert("INSERT INTO http_sequence(guid,`group`,`type`,`name`,remark,jobName,frequency,jobType)  VALUES("
			+"#{httpSequence.guid},#{httpSequence.group},#{httpSequence.type},"
			+ "#{httpSequence.name},#{httpSequence.remark},#{httpSequence.jobName},#{httpSequence.frequency},#{httpSequence.jobType})")
	void insert(@Param("httpSequence") HttpSequence httpSequence);

	@Update("update http_sequence t set t.`group`=#{httpSequence.group},t.`type`=#{httpSequence.type},"
			+ "t.`name`=#{httpSequence.name},t.remark=#{httpSequence.remark},t.frequency=#{httpSequence.frequency} WHERE t.`guid`= #{httpSequence.guid}")
	void update(@Param("httpSequence")HttpSequence httpSequence);
	
	
	@Select("<script>" +
			"SELECT * FROM http_sequence" +
			"<where>" +
			"<if test=\"httpSequence.group!=null and httpSequence.group!=''\">and `group` = #{httpSequence.group}</if>" +
			"<if test=\"httpSequence.name!=null and httpSequence.name!=''\">and name like concat('%',#{httpSequence.name},'%')</if>" +
			"<if test=\"httpSequence.jobType!=null and httpSequence.jobType!=''\">and jobType = #{httpSequence.jobType}</if>" +
			" and archived = 0 " +
			"<if test=\"httpSequence.guid!=null and httpSequence.guid!=''\">and guid = (select pguid from http_request where archived = 0 and guid = #{httpSequence.guid})</if>" +
			"</where>" +
			"</script>")
	List<HttpSequence> selectAllJob(@Param("httpSequence") HttpSequence httpSequence);

	@Select("<script>" +
			"SELECT count(*) total FROM http_sequence" +
			"<where>" +
			"<if test=\"httpSequence.group!=null and httpSequence.group!=''\">and `group` = #{httpSequence.group}</if>" +
			"<if test=\"httpSequence.name!=null and httpSequence.name!=''\">and name like concat('%',#{httpSequence.name},'%')</if>" +
			"<if test=\"httpSequence.jobType!=null and httpSequence.jobType!=''\">and jobType = #{httpSequence.jobType}</if>" +
			" and archived = 0 " +
			"<if test=\"httpSequence.guid!=null and httpSequence.guid!=''\">and guid = (select pguid from http_request where archived = 0 and guid = #{httpSequence.guid})</if>" +
			"</where>" +
			"</script>")
	Map<String,Long> queryAllJobCount(@Param("httpSequence") HttpSequence httpSequence);

	@Select("SELECT * FROM http_sequence where archived = 0")
	List<HttpSequence> selectAll();
	
	@Select("select t.* from http_sequence t where t.guid=#{guid}")
	HttpSequence getByGuid(@Param("guid")String guid);

	@Update("UPDATE http_sequence t SET t.`enabled` = #{httpSequence.enabled} WHERE t.`guid`= #{httpSequence.guid} ")
	void updateEnabled(@Param("httpSequence")HttpSequence httpSequence);

	@Update("UPDATE http_sequence t SET t.`archived` = 1 WHERE t.`guid`= #{guid} ")
	void archived(@Param("guid")String guid);

	@Select("select t.guid,t.`group`,t.type,t.`name`,t.frequency,t.enabled from http_sequence t left join http_system a on t.group = a.name left join http_authority b on a.id = b.system_id where t.archived = 0 and b.uuid = #{uuid}")
	List<Map<String,Object>> selectMonitorList(@Param("uuid")String uuid);



	
	@Insert("INSERT INTO http_system(`name`)  VALUES(#{httpSystem.name})")
	void insertSystem(@Param("httpSystem") HttpSystem httpSystem);
	

	
	@Select("select * from http_system")
	List<HttpSystem> selectAllSystem();

	@Select("select name label,guid id,0 parentId from http_sequence where archived = 0 and jobType= 'MONITOR' union all select a.name label,a.guid id,a.pguid parentId from http_request a join http_sequence b on a.pguid = b.guid where a.archived = 0 and b.archived = 0 and b.jobType= 'MONITOR'")
	List<TreeItem> queryTreeData();
	
}
